 /**
 * Description: 系统角色业务实现类
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-10 下午 19:40:19
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-10   xk       0.0.1     Initial
 */
package com.metro.paperless.sys.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.paperless.common.beans.constant.CommonConstant;
import com.metro.paperless.common.beans.constant.DicTypeEnum;
import com.metro.paperless.common.utils.DicUtil;
import com.metro.paperless.common.utils.UserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.paperless.sys.mapper.ISysRoleMapper;
import com.metro.paperless.sys.service.inf.ISysRoleService;
import com.metro.paperless.sys.beans.vo.SysRole;

import java.util.List;

import static com.metro.paperless.common.beans.constant.CommonConstant.ROLE_SUPER_MANAGER;

 /**
 * 系统角色业务实现类<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-10]
 */
@Service("sysRoleService")
public class SysRoleService extends BaseServiceSupport<SysRole> implements ISysRoleService{
    //系统角色Mapper
    @Autowired
    private ISysRoleMapper sysRoleMapper;

    @Override
    public IMapper getMapper() {
        return sysRoleMapper;
    }

     @Override
     public IResult validateUserHasRole(String roleIds) {
         if(UserUtil.hasRole(ROLE_SUPER_MANAGER)){
             return ResultCode.SUCCESS;
         }else{
             String superRoleId = DicUtil.getIdStr(DicTypeEnum.ROLE_ID_CODE, CommonConstant.ROLE_SUPER_MANAGER);
             if(!roleIds.contains(superRoleId)){
                 return ResultCode.SUCCESS;
             }
         }
         return ResultCode.NOT_SUPER_MANAGER;
     }

     @Override
     public List<SysRole> queryRoleByUserId(Long userId) {
         return sysRoleMapper.queryRoleByUserId(userId);
     }

     @Override
     public SysRole getRoleByCode(String code) {
         if (StringUtils.isBlank(code)) {
             return null;
         }
         SysRole param = new SysRole();
         param.setCode(code);
         List<SysRole> resultList = this.queryForList(param);

         if (resultList != null && !resultList.isEmpty()) {
             return resultList.get(0);
         }

         return null;
     }

     @Override
    public IResult save(SysRole entity) {
        try {
            sysRoleMapper.save(entity);
            DicUtil.removeKeyFromCache(DicTypeEnum.role);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增系统角色失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(SysRole entity) {
        try {
            sysRoleMapper.update(entity);
            DicUtil.removeKeyFromCache(DicTypeEnum.role);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改系统角色失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            sysRoleMapper.delete(id);
            DicUtil.removeKeyFromCache(DicTypeEnum.role);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            sysRoleMapper.deleteByIds(ids);
            DicUtil.removeKeyFromCache(DicTypeEnum.role);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
}